#!/bin/sh

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"

# Get translations
ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
case $ENVIRONMENT_LANG in
	de)	export LANGUAGE="german" ;;
	en)	export LANGUAGE="english" ;;
	es)	export LANGUAGE="spanish" ;;
	fr)	export LANGUAGE="french" ;;
	it)	export LANGUAGE="italian" ;;
	nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
	nl)	export LANGUAGE="dutch" ;;
	nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
	ru)	export LANGUAGE="russian" ;;
	*)	export LANGUAGE="english" ;; #english for contries without translation
esac
. $APPDIR/locale/english.po #always run to fill gaps in translation
[ -f "$APPDIR/locale/${LANGUAGE}.po" ] && . $APPDIR/locale/${LANGUAGE}.po 2> /dev/null


build_list() {
for LINE in `cat /etc/rc.firewall | grep open | grep -v '#' | cut -f 2 -d ' '`
do
 PORT="`echo $LINE | cut -f 1 -d ':'`"
 SERVICE="`cat /etc/services | grep "$PORT" | tr "\t" ' ' | tr -s ' ' | cut -f 1 -d ' ' | head -n 1`"
 [ "$SERVICE" = "" ] && SERVICE="unknown"
 echo "${LINE}|${SERVICE}"
done
}
case $1 in
 -buildlist)
 build_list
 ;;
 -start)
 service iptables start
 [ $? -eq 0 ] && Xdialog --title "$LOC_START_TITLE" --msgbox "$LOC_STARTMSG" 0 0
 ;;
 -stop)
 service iptables stop
 [ $? -eq 0 ] && Xdialog --title "$LOC_STOP_TITLE" --msgbox "$LOC_STOPMSG" 0 0
 ;;
 -restart)
 service iptables restart
 [ $? -eq 0 ] && Xdialog --title "$LOC_RESTART_TITLE" --msgbox "$LOC_RESTARTMSG" 0 0
 ;;
 -delete)
 cat /etc/rc.firewall | grep '#' > /tmp/firewall
 cat /etc/rc.firewall | grep -v '#' | grep -v "$2" >> /tmp/firewall
 mv -f /tmp/firewall /etc/rc.firewall
 ;;
 -add)
 cat /etc/services | grep -v '#' | tr '/' ':' | tr "\t" ' ' | tr -s ' ' | tr ' ' '|' | cut -f 1-2 -d '|' > /tmp/services
 export ADD_DLG="
 <window title=\"$LOC_OPEN_PORT\" icon_name=\"gtk-open\">
  <vbox>
   <button>
    <label>$LOC_CUSTOM</label>
    <action>$APPDIR/functions -custom</action>
    <action type=\"EXIT\">CUSTOM</action>
   </button>
   <tree exported_column=\"1\">
    <height>400</height>
    <width>250</width>
    <label>$LOC_ADDTREE</label>
    <variable>PORT</variable>
    <action>echo open \$PORT >> /etc/rc.firewall</action>
    <action type=\"EXIT\">ADD</action>
    <input>cat /tmp/services</input>
   </tree>
   <hbox>
    <button cancel></button>
    <button>
     <input file stock=\"gtk-add\"></input>
     <label>$LOC_OPEN_PORT</label>
     <action>echo open \$PORT >> /etc/rc.firewall</action>
     <action type=\"EXIT\">ADD</action>
    </button>
   </hbox>
  </vbox>
 </window>
 "
 gtkdialog3 --program ADD_DLG
 ;;
 -custom)
 export CUSTOM_DLG="
 <window title=\"$LOC_CUSTOM_WINDOW\" icon_name=\"gtk-add\" window-position=\"2\">
  <vbox>
   <entry>
    <variable>PORT</variable>
   </entry>
   <hbox>
    <checkbox>
     <label>tcp</label>
     <variable>TCP</variable>
     <default>true</default>
    </checkbox>
    <checkbox>
     <label>udp</label>
     <variable>UDP</variable>
     <default>true</default>
    </checkbox>
   </hbox>
   <hbox>
    <button cancel></button>
    <button>
     <input file stock=\"gtk-add\"></input>
     <label>$LOC_OPEN_PORT</label>
    </button>
   </hbox>
  </vbox>
 </window>
 "
 gtkdialog3 --program CUSTOM_DLG > /tmp/customport
 . /tmp/customport
 if [ "$EXIT" = "Open Port" ];then
  if [ ! "$PORT" = "" ];then
   if [ ! "`echo $PORT | grep [0-9]`" = "" ];then
    [ "$TCP" = "true" ] && echo "open $PORT:tcp" >> /etc/rc.firewall
    [ "$UDP" = "true" ] && echo "open $PORT:udp" >> /etc/rc.firewall
   fi
  fi
 fi
 ;;
 *)
 echo "Not written yet"
 ;;
esac
